package com.jinxin.phonecloudmusic.myapplication.fragment.fra;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jinxin.phonecloudmusic.myapplication.R;
import com.jinxin.phonecloudmusic.myapplication.app.JxscApp;
import com.jinxin.phonecloudmusic.myapplication.base.BaseFragment;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.Play;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.SongList;
import com.jinxin.phonecloudmusic.myapplication.event.MusicControlEvent;
import com.jinxin.phonecloudmusic.myapplication.presenter.IPresenter.IMainPagePresenter;
import com.jinxin.phonecloudmusic.myapplication.presenter.PresenterImpl.IMainPagePresentImp;
import com.jinxin.phonecloudmusic.myapplication.utils.RxBus;
import com.jinxin.phonecloudmusic.myapplication.view.IMusicImageFraView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

/**
 * Created by ZJ on 2017/10/9 0009.
 */

public class PlayMusicImageFragment extends BaseFragment implements IMusicImageFraView {
    @BindView(R.id.iv_current_music)
    ImageView ivCurrentMusic;
    private List<SongList> allHotSongs=new ArrayList<>();
    private int pos;
    private IMainPagePresenter iMainPagePresenter;
    @Override
    public View inflate(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fra_music_img, container, false);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*if (null!=getArguments()){
            pos = getArguments().getInt("pos");
            allHotSongs = (List<SongList>) getArguments().getSerializable("allSongs");
        }*/
        iMainPagePresenter = new IMainPagePresentImp(this);
        //iMainPagePresenter.fraPlayMusic("play", allHotSongs.get(pos).getSong_id());
    }

    @Override
    public void initData() {
        Log.e("----initDATA","initData");
        //上下首
        RxBus.getDefault().register(MusicControlEvent.class).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<MusicControlEvent>() {
            @Override
            public void accept(MusicControlEvent event) throws Exception {
                if ((event.getTag()) == 9) {//上下首
                    if (null!=ivCurrentMusic&&!((Play)(event.getObj1())).getSongInfo().getPic_premium().isEmpty())
                    Picasso.with(JxscApp.getContext()).load(((Play)(event.getObj1())).getSongInfo().getPic_premium()).placeholder(R.drawable.background).error(R.drawable.background).into(ivCurrentMusic);
                }
            }
        });
    }

    @Override
    public void updateView(Play play) {
        //Picasso.with(JxscApp.getContext()).load(play.getSongInfo().getPic_premium()).placeholder(R.drawable.background).error(R.drawable.background).into(ivCurrentMusic);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("onResume","onResume");
    }
}
